package com.example.home.rubbish.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.home.R;
import com.example.home.rubbish.activity.RubbishNews;
import com.example.home.rubbish.bean.RubbishNewsBean;

import java.util.List;

import static com.example.home.tool.StaticActi.ip;

public class RubbishNewsAdapter extends RecyclerView.Adapter<RubbishNewsAdapter.MyHoldy> {
    private Context context;
    private List<RubbishNewsBean.RowsBean> list;

    public RubbishNewsAdapter(Context context, List<RubbishNewsBean.RowsBean> list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public MyHoldy onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_rubb,null);
        return new RubbishNewsAdapter.MyHoldy(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyHoldy holder, int position) {
        holder.title.setText(list.get(position).getTitle());
        holder.createTime.setText(list.get(position).getCreateTime());
        Glide.with(context).load(ip+list.get(position).getImgUrl()).into(holder.imgUrl);
        holder.rootView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, RubbishNews.class);
                intent.putExtra("RnewsId",String.valueOf(list.get(position).getId()));
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    public class MyHoldy extends RecyclerView.ViewHolder {
        private RelativeLayout rootView;
        private TextView title;
        private TextView createTime;
        private ImageView imgUrl;


        public MyHoldy(@NonNull View itemView) {
            super(itemView);
            rootView = itemView.findViewById(R.id.rootView);
            title = itemView.findViewById(R.id.title);
            createTime = itemView.findViewById(R.id.createTime);
            imgUrl = itemView.findViewById(R.id.imgUrl);
        }
    }
}
